﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption

Partial Class Reminder
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadReminder()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadReminder()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadReminder()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadReminder()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadReminder()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadReminder()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadReminder()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadReminder()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadReminder()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadReminder()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadReminder()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadReminder()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadReminder()
    End Sub

#End Region

    Private Sub LoadReminder()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        If Session.Item("StaffType").ToString = "2" Then
            objDB.dbVariable.SQLString = "SELECT R.ReminderId, R.AffectedId, CASE R.ReminderType WHEN '1' " & _
                                            "THEN 'Rent Transaction' WHEN '2' THEN 'Ren Report' WHEN '3' THEN 'Ren Report Reminder' ELSE '' END AS Type, " & _
                                            "R.CreateDate, CASE R.ReminderType WHEN '1' THEN RT.OpenFileRef WHEN '3' " & _
                                            "THEN RC.Name WHEN '2' THEN RR.Title ELSE '' END AS AffectedValue, " & _
                                            "ISNULL(p.UnitNumber + ', ' + d.Road + ', ' + o.Location + ', ' + s.State + ', ' + y.Country, 'N.A.') as 'Address' " & _
                                            "FROM AW_Reminder R LEFT JOIN " & _
                                            "AW_Property p ON R.PropertyId=p.PropertyId LEFT JOIN AW_Road d ON p.RoadId = d.RoadId " & _
                                            "LEFT JOIN AW_Location o ON d.LocationId = o.LocationId LEFT JOIN AW_State s ON " & _
                                            "o.StateId = s.StateId LEFT JOIN AW_Country y ON s.CountryId = y.CountryId LEFT JOIN " & _
                                            "AW_RentTransaction RT ON R.AffectedId = RT.TransactionId LEFT JOIN AW_RenReport RR ON R.AffectedId = RR.RenReportId LEFT JOIN " & _
                                            "AW_RenContact RC ON R.AffectedId = RC.RenContactId WHERE R.StatusDelete=0 AND R.RenId=@StaffId" & lblHidSortType.Text
            objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text.Trim))
        Else
            objDB.dbVariable.SQLString = "SELECT R.ReminderId, R.AffectedId, CASE R.ReminderType WHEN '1' " & _
                                "THEN 'Rent Transaction' WHEN '2' THEN 'Ren Report' WHEN '3' THEN 'Ren Report Reminder' ELSE '' END AS Type, " & _
                                "R.CreateDate, CASE R.ReminderType WHEN '1' THEN RT.OpenFileRef WHEN '3' " & _
                                "THEN RC.Name WHEN '2' THEN RR.Title ELSE '' END AS AffectedValue, " & _
                                "ISNULL(p.UnitNumber + ', ' + d.Road + ', ' + o.Location + ', ' + s.State + ', ' + y.Country, 'N.A.') as 'Address' " & _
                                "FROM AW_Reminder R LEFT JOIN " & _
                                "AW_Property p ON R.PropertyId=p.PropertyId LEFT JOIN AW_Road d ON p.RoadId = d.RoadId " & _
                                "LEFT JOIN AW_Location o ON d.LocationId = o.LocationId LEFT JOIN AW_State s ON " & _
                                "o.StateId = s.StateId LEFT JOIN AW_Country y ON s.CountryId = y.CountryId LEFT JOIN " & _
                                "AW_RentTransaction RT ON R.AffectedId = RT.TransactionId LEFT JOIN AW_RenReport RR ON R.AffectedId = RR.RenReportId LEFT JOIN " & _
                                "AW_RenContact RC ON R.AffectedId = RC.RenContactId WHERE R.StatusDelete=0" & lblHidSortType.Text
        End If
    
        objDB.FillData(TempDT, "AW_Reminder", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        'to filter those more than Active Reminder Interval
        Dim intReminderDays As Integer = getReminderDays()
        Dim dtView As Data.DataView = New Data.DataView
        With dtView
            .Table = TempDT
            '.RowFilter = "CreateDate >=#" & Now.AddDays(-intReminderDays) & "#"
        End With

        TempDT = dtView.ToTable

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = dtView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repReminder.DataSource = objPaged
        repReminder.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Private Function getReminderDays() As Integer
        Dim intReminderDays As Integer = 0
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT IntActiveReminder FROM AW_Setting"
        objDB.FillData(TempDT, "AW_Setting", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        If TempDT.Rows.Count > 0 Then
            intReminderDays = CInt(TempDT.Rows(0).Item("IntActiveReminder").ToString)
        End If

        Return intReminderDays
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
            lblStaffType.Text = Session("StaffType").ToString
        End If

        If Not Page.IsPostBack Then
            LoadReminder()
        End If
    End Sub

    Protected Sub repReminder_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repReminder.ItemCommand
        If e.CommandName.ToUpper = "REFERITEM" Then
            Dim AffectedId As String = CType(repReminder.Items(e.CommandArgument).FindControl("lblRepAffectedId"), Label).Text
            Dim RepType As String = CType(repReminder.Items(e.CommandArgument).FindControl("lblRepType"), Label).Text
            If lblStaffType.Text = "2" Then
                'Staff
                If RepType.ToUpper = "REN REPORT" Then
                    Response.Redirect("RenReportEntry.aspx?id=" & SymmetricEncryptToHex(AffectedId, "augment"))
                ElseIf RepType.ToUpper = "REN REPORT REMINDER" Then
                    Response.Redirect("RenReportView.aspx?id=" & SymmetricEncryptToHex(AffectedId, "augment"))
                End If
            Else
                If RepType.ToUpper = "REN REPORT" Then
                    Response.Redirect("RenReportEntry.aspx?id=" & SymmetricEncryptToHex(AffectedId, "augment"))
                ElseIf RepType.ToUpper = "REN REPORT REMINDER" Then
                    Response.Redirect("RenReportView.aspx?id=" & SymmetricEncryptToHex(AffectedId, "augment"))
                Else
                    Session.Add("Reminder", AffectedId)
                    Response.Redirect("transcompletelist.aspx")
                End If
            End If
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "UPDATE AW_Reminder SET StatusDelete=1 WHERE ReminderId=@ReminderId"
            objDB.AddParameter("ReminderId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repReminder.Items(e.CommandArgument).FindControl("lblRepReminderUID"), Label).Text))
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            lblMsgSuccessful.Text = "Announcement Entry Deleted"
            lblMsgSuccessful.Visible = True
            LoadReminder()
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkAddress_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddress.Click
        If lnkAddress.Text = "Address▼" Then
            lnkAddress.Text = "Address▲"
        Else
            lnkAddress.Text = "Address▼"
        End If

        lblHidSortType.Text = " ORDER BY Address " & SortType(lnkAddress.Text) & ", AffectedValue " & _
                            SortType(lnkAffected.Text) & ", Type " & SortType(lnkType.Text) & ", CreateDate " & SortType(lnkCreateDate.Text)
        LoadReminder()
    End Sub

    Protected Sub lnkAffected_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAffected.Click
        If lnkAffected.Text = "Reminder On▼" Then
            lnkAffected.Text = "Reminder On▲"
        Else
            lnkAffected.Text = "Reminder On▼"
        End If

        lblHidSortType.Text = " ORDER BY AffectedValue " & SortType(lnkAffected.Text) & ", Address " & _
                            SortType(lnkAddress.Text) & ", Type " & SortType(lnkType.Text) & ", CreateDate " & SortType(lnkCreateDate.Text)
        LoadReminder()
    End Sub

    Protected Sub lnkType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkType.Click
        If lnkType.Text = "Reminder Type▼" Then
            lnkType.Text = "Reminder Type▲"
        Else
            lnkType.Text = "Reminder Type▼"
        End If

        lblHidSortType.Text = " ORDER BY Type " & SortType(lnkType.Text) & ", Address " & _
                            SortType(lnkAddress.Text) & ", AffectedValue " & SortType(lnkAffected.Text) & ", CreateDate " & SortType(lnkCreateDate.Text)
        LoadReminder()
    End Sub

    Protected Sub lnkCreateDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateDate.Click
        If lnkCreateDate.Text = "Date▼" Then
            lnkCreateDate.Text = "Date▲"
        Else
            lnkCreateDate.Text = "Date▼"
        End If

        lblHidSortType.Text = " ORDER BY CreateDate " & SortType(lnkCreateDate.Text) & ", Address " & _
                            SortType(lnkAddress.Text) & ", AffectedValue " & SortType(lnkAffected.Text) & ", Type " & SortType(lnkType.Text)
        LoadReminder()
    End Sub
End Class
